# OpenBMC编译环境 Dockerfile
# 基于Ubuntu 24.04，配置完整的Yocto/BitBake构建环境

FROM ubuntu:24.04

# 设置非交互式安装，避免tzdata等包的交互提示
ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=UTC

# 配置国内apt源（使用阿里云镜像）
RUN sed -i 's|http://archive.ubuntu.com|http://mirrors.aliyun.com|g' /etc/apt/sources.list.d/ubuntu.sources && \
    sed -i 's|http://security.ubuntu.com|http://mirrors.aliyun.com|g' /etc/apt/sources.list.d/ubuntu.sources || \
    (echo "deb http://mirrors.aliyun.com/ubuntu/ noble main restricted universe multiverse" > /etc/apt/sources.list && \
     echo "deb http://mirrors.aliyun.com/ubuntu/ noble-updates main restricted universe multiverse" >> /etc/apt/sources.list && \
     echo "deb http://mirrors.aliyun.com/ubuntu/ noble-backports main restricted universe multiverse" >> /etc/apt/sources.list && \
     echo "deb http://mirrors.aliyun.com/ubuntu/ noble-security main restricted universe multiverse" >> /etc/apt/sources.list)

# 配置apt重试和超时设置
RUN echo 'Acquire::Retries "3";' > /etc/apt/apt.conf.d/80-retries && \
    echo 'Acquire::http::Timeout "30";' >> /etc/apt/apt.conf.d/80-retries && \
    echo 'Acquire::https::Timeout "30";' >> /etc/apt/apt.conf.d/80-retries

# 安装OpenBMC/Yocto构建所需的所有依赖包
RUN apt-get update && apt-get install -y \
    # 基础工具
    git \
    python3 \
    python3-pip \
    python3-pexpect \
    python3-git \
    python3-jinja2 \
    python3-subunit \
    # 编译工具链
    build-essential \
    gcc \
    g++ \
    gcc-multilib \
    g++-multilib \
    # Yocto必需工具
    chrpath \
    cpio \
    diffstat \
    gawk \
    texinfo \
    wget \
    # 压缩和打包工具
    lz4 \
    zstd \
    bzip2 \
    xz-utils \
    # 库依赖
    libssl-dev \
    libncurses5-dev \
    zlib1g-dev \
    liblz4-tool \
    libsdl2-dev \
    # 其他工具
    vim \
    curl \
    locales \
    file \
    sudo \
    iputils-ping \
    tree \
    && rm -rf /var/lib/apt/lists/*

# 配置locale（Yocto需要UTF-8环境）
RUN locale-gen en_US.UTF-8
ENV LANG=en_US.UTF-8
ENV LANGUAGE=en_US:en
ENV LC_ALL=en_US.UTF-8

# 创建非root用户（Yocto/BitBake禁止以root身份运行）
# 用户名：openbmc，使用动态UID（避免冲突）
RUN useradd -m -s /bin/bash openbmc && \
    echo "openbmc ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers

# 切换到openbmc用户
USER openbmc
WORKDIR /home/openbmc

# 配置Git（BitBake需要）
RUN git config --global user.name "TacoJay" && \
    git config --global user.email "zhangchao60@huawei.com" && \
    git config --global color.ui false

# 设置工作目录
WORKDIR /workspace

# 暴露环境变量，供后续脚本使用
ENV OPENBMC_SOURCE=/workspace/data/openbmc_source
ENV BUILD_OUTPUT=/workspace/data/build

# 默认命令
CMD ["/bin/bash"]

